
<div class="section" id="khuvuclist">

	<div class="section-title">Quản lý lập phiếu giao báo</div>
    
    <div class="section-content padding1">
    	<div class="button right">
            <a class="button save" onclick="save()">Lưu</a>
            <a class="button cancel" href="?route=deliman/phieugiaobao">Bỏ qua</a>  
            
        </div>
        <div class="clearer">^&nbsp;</div>
    
    	<form name="frm" id="frm" action="<?php echo $action?>" method="post" enctype="multipart/form-data">
        
        	<input type="hidden" name="phieugiaobaoid" value="<?php echo $item['phieugiaobaoid']?>">
            <input type="hidden" id="nhanvienquanlyid" name="nhanvienquanlyid" value="<?php echo $item['nhanvienquanlyid']?>">
        	<div id="error" class="error" style="display:none"></div>
            
        	
            	
            <div id="fragment-thongtin">
            	<table class="table-form">
                	<tr>
                    	<td><label>Mã phiếu:</label></td>
                        <td><input type="text" name="maphieugiaobao" value="<?php echo $item['maphieugiaobao']?>" class="text <?php echo $class?>"  size=60 <?php echo $readonly?>/></td>
                    </tr>
                    
                    <tr>
                    	<td><label>Ngày lập:</label></td>
                        <td><input type="text" name="ngaylap" value="<?php echo $this->date->formatMySQLDate($item['ngaylap'])?>" class="text ben-datepicker"/></td>
                    </tr>
                    <tr>
                    	<td><label>Khu vực:</label></td>
                        <td>
                        	
                        	<select id="khuvucgiaoid" name="khuvucgiaoid" <?php echo $disabled?> >
                            	<?php foreach($data_khuvuquanly as $khuvuc){ ?>
                                <option value="<?php echo $khuvuc['khuvucid']?>"><?php echo $khuvuc['tenkhuvuc']?></option>
                                <?php } ?>
                            </select>
                            
                            <?php if($disabled !=''){ ?>
                            <input type="hidden" name="khuvucgiaoid" value="<?php echo $item['khuvucgiaoid']?>" />
                           	<?php }?>
                        </td>
                    </tr>
                    <tr>
                    	<td><label>Nhân viên nhận:</label></td>
                        <td>
                        	<select id="nhanviengiaobaoid" name="nhanviengiaobaoid" <?php echo $disabled?> >
                            	<option value=""></option>
                                
                            </select>
                           	<?php if($disabled !=''){ ?>
                            <input type="hidden" name="nhanviengiaobaoid" value="<?php echo $item['nhanviengiaobaoid']?>" />
                           	<?php }?>
                        </td>
                    </tr>
                    <tr>
                    	<td><label>Ghi chú:</label></td>
                        <td><textarea name="ghichu" style="width:50%"><?php echo $item['ghichu']?></textarea></td>
                    </tr>
                    
                </table>
                
            </div>
            <div id="tinhhinhgiaobao" class="khuvuc treeindex">
				
                
                  <table class="example data-table table-form">
                    <thead>
                        <tr class="tr-head">
                        	
                            <th>Loại báo</th>
                            <th>Tên kỳ báo</th>
                            <th>Ngày phát hành</th>
                            <th>Số lượng giao</th>
                            <th>Số lượng tồn</th>
                            <th>Đã giao</th>
                            <th>Chỉ tiêu</th>                 
                        </tr>
                    </thead>
                    <tbody id="listgiaobao">  
                   
                	</tbody>
                    <tfoot>
                    	<tr>
                        	<td></td>
                            <td></td>
                            <td>Tổng số lượng đã  giao</td>
                            <td class="number">
                            	<span id="lblTongSongLuongGiao"><?php echo $this->string->numberFormate($item['tongsoluonggiao'])?></span><input type="hidden" id="tongsoluonggiao" name="tongsoluonggiao" value="<?php echo $item['tongsoluonggiao']?>"/>
                                </td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>
                    </tfoot>
                </table>
                
             
            </div> 
            
                
            
            
        </form>
    
    </div>
    
</div>

<script language="javascript">
$(document).ready(function(e) {
	$('#khuvucgiaoid').val("<?php echo $item['khuvucgiaoid']?>").change();
    //loadChiTietPhieuGiao("<?php echo $item['khuvucgiaoid']?>","<?php echo $item['phieugiaobaoid']?>");
	
});

function loadChiTietPhieuGiao(khuvucid,phieugiaobaoid)
{
	
	$('#listgiaobao').load("?route=deliman/phieugiaobao/layTinhTangBaoTheoKhuVucForm&khuvucid="+khuvucid+"&phieugiaobaoid="+phieugiaobaoid,function(){
		numberReady();
		$('.soluong').keyup(function(e) {	
			var index = $(this).attr('ref');
			var chitieu = $('#chitieu' + index).html().replace(/,/g,"");
			var soluong = this.value.replace(/,/g,"");
			var dagiao = $('#dagiao' + index).val();
			var datra = $('#datra' + index).val();
			var daphanphoi = $('#daphanphoi' + index).val();
			//alert(dagiao);
			var soluongton = Number(chitieu) - Number(soluong) - Number(dagiao) - daphanphoi - datra;
			if(soluongton >=0)
				$('#soluongton' + $(this).attr('ref')).html(formateNumber(soluongton));
			else
			{
				this.value = '';
				alert("Bạn không được nhập quá số lượng tồn");
			}
			var sum = 0;
			$('.soluong').each(function(index, element) {
				var soluong = this.value.replace(/,/g,"");
				sum += Number(soluong)
			});
			$('#lblTongSongLuongGiao').html(formateNumber(sum));
			$('#tongsoluonggiao').val(sum);
		});
	});
}

$('#khuvucgiaoid').change(function(e) {
    loadChiTietPhieuGiao(this.value,"<?php echo $item['phieugiaobaoid']?>");
	
	$('#nhanviengiaobaoid').load("?route=deliman/phieugiaobao/getNhanVienGiaoBaoTheoKhuVuc&khuvucid="+this.value,function(){
		$('#nhanviengiaobaoid').val('<?php echo $item["nhanviengiaobaoid"]?>');
	});
});

/*$('#btnTimKyBao').click(function(e) {
    var result = window.showModalDialog("index.php?route=deliman/kybao&dialog=true","", "dialogWidth:1000px; dialogHeight:450px; center:yes");
	
	var arr = $('#selectkybao').val().split(',');
	for(i=0;i<arr.length;i++)
	{
		//alert(arr[i]);
		if(arr[i]!="")
		{
			giaobao.addRow("",arr[i],0);
		}
	}
});

$('#btnXoaDong').click(function(e) {
    $('.inputchk').each(function(index, element) {
        if(this.checked)
		{
			index = $(this).attr('ref');
			giaobao.remove(index);
		}
    });
});

function GiaoBao()
{
	this.index = 0;
	this.addRow = function(id,kybaoid,soluong)
	{
		$.getJSON("?route=deliman/phieugiaobao/getKyBao",
		{
			col:'kybaoid',
			val:kybaoid,
			nhanvienid:$('#nhanvienquanlyid').val()
		},
		function(data)
		{
			var cellchk = '<td><input type="checkbox" class="inputchk" ref="'+ giaobao.index +'"><input type="hidden" id="id-'+ giaobao.index +'" name="id['+ giaobao.index +']" value="'+id+'" ref="'+ giaobao.index +'"></td>';	
			var cellloaibao = '<td><input type="hidden" id="loaibaoid-'+ giaobao.index +'" name="loaibaoid['+ giaobao.index +']" value="'+data.kybaos[0].loaibaoid+'" ref="'+ giaobao.index +'">'+data.kybaos[0].tenloaibao+'</td>';
			var cellkybao = '<td><input type="hidden" id="kybaoid-'+ giaobao.index +'" name="kybaoid['+ giaobao.index +']" value="'+data.kybaos[0].kybaoid+'" ref="'+ giaobao.index +'">'+data.kybaos[0].tenkybao+'</td>';
			var cellngayphathanh = '<td>'+data.kybaos[0].ngayphathanh+'</td>';
			var cellsoluong = '<td class="number"><input type="hidden" id="dagiao'+ giaobao.index +'"  value="'+data.kybaos[0].tongdagiao+'" ref="'+ giaobao.index +'"><input type="text" id="soluong-'+ giaobao.index +'" name="soluong['+ giaobao.index +']" value="'+soluong+'" class="text number soluong" ref="'+ giaobao.index +'"></td>';
			var cellsoluongton = '<td class="number" id="soluongton'+ giaobao.index +'">'+formateNumber(data.kybaos[0].soluongton)+'</td>';
			var cellchitieu = '<td class="number" id="chitieu'+ giaobao.index +'">'+formateNumber(data.kybaos[0].tongchitieu)+'</td>';
			
			row = cellchk;
			row += cellloaibao;
			row += cellkybao;
			row += cellngayphathanh;
			row += cellsoluong;
			row += cellsoluongton;
			row += cellchitieu;
			$('#listgiaobao').append('<tr id="row'+giaobao.index+'">'+row+'</tr>');
			giaobao.index++;
			numberReady()
			$('.soluong').keyup(function(e) {
				var index = $(this).attr('ref');
                var chitieu = $('#chitieu' + index).html().replace(/,/g,"");
				var soluong = this.value.replace(/,/g,"");
				var dagiao = $('#dagiao' + index).val();
				
				var soluongton = Number(chitieu) - Number(soluong) - Number(dagiao);
				
				$('#soluongton' + $(this).attr('ref')).html(formateNumber(soluongton));
            });
			
		});
	}
	
	this.remove = function(pos)
	{
		//alert($('#delgiaobao').val());
		$('#delgiaobao').val($('#delgiaobao').val() + $('#id-'+pos).val()+',');
		$('#row'+pos).remove();	
	}
}
var giaobao = new GiaoBao();*/
<!--thực hiện chức năng save dữ liệu-->
function save()
{
	$.blockUI({ message: "<h1>Please wait...</h1>" }); 
	
	$.post("?route=deliman/phieugiaobao/save", $("#frm").serialize(),
		function(data){
			if(data == "true")
			{
				window.location = "?route=deliman/phieugiaobao";
			}
			else
			{
			
				$('#error').html(data).show('slow');
				$.unblockUI();
				
			}
			
		}
	);
}
</script>
